<?php


namespace app\seller\controller;
use think\Config;
use think\Db;
use think\db\exception\DbException;


class Wallet extends Base
{
    public function balance()
    {
        $service_charge = Config::get('site.withdrawal_commission'); //
        return $this->success('ok', [
            'balance' => $this->shop['balance'],
            'service_charge' => $service_charge,
        ]);
    }

    public function withdrawal()
    {
        $money = floatval(input('money'));
        $card_name = input('card_name');
        $card_no = input('card_no');
        $username = input('username');
        if (!$card_name || !$username || !$card_no) {
            return $this->error('请选择到账账户');
        }
        if (!$money || $money <= 0) {
            return $this->error('请输入提现金额');
        }
        $before_money = Db::name('category')->where([
            'id' => $this->shop_id,
            "seller_id" => $this->seller_id,
        ])->value('balance');
        if ($before_money < $money) {
            return $this->error('余额不足');
        }

        $service_charge = Config::get('site.withdrawal_commission'); //
        $receipt_money = round($money - ($money * $service_charge / 100), 2);


        Db::startTrans();
        try {
            $rs2 = Db::name('withdrawal')->insert([
                'category_id' => $this->shop_id,
                'card_name' => $card_name,
                'username' => $username,
                'card_no' => $card_no,
                'state' => 0,
                'create_time' => time(),
                'money' => $money,
                'after_money' => $before_money - $money,
                'receipt_money' => $receipt_money,
                'service_charge' => $money - $receipt_money,
            ]);
            $rs1 = \app\admin\model\CategoryMoneyLog::_do($this->shop_id, -$money, 1, '商家申请提现：' . $card_name . "，" . $card_no . "，" . $username);
            if ($rs2 && $rs1) {
                Db::commit();
                return $this->success('提现成功');
            }
            throw new DbException('提现失败');
        } catch (DbException $e) {
            Db::rollback();
            return $this->error('提现失败', $e);
        }

    }

    // 提现记录
    public function withdrawalLog()
    {
        $map['category_id'] = $this->shop_id;
        if(input('?state')) $map['state'] = input('state');

        $list = Db::name('withdrawal')->where($map)->paginate()->each(function($item, $key){
            $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
            $item['examine_time'] = $item['examine_time'] ? date('Y-m-d H:i:s', $item['examine_time']) : 0;
            if($item['state'] == 1) {
                $item['state_text'] = '通过';
            }else if($item['state'] == 2) {
                $item['state_text'] = '拒绝';
            }else{
                $item['state_text'] = '待审核';
            }

            return $item;
        });

        return $this->success('操作成功', $list);
    }

    // 分账记录
    public function shareMoneyLog()
    {
        $map['category_id'] = $this->shop_id;
        $map['status'] = 1;

        $list = Db::name('profit_share_log')->where($map)->paginate()->each(function($item, $key){
            $item['createtime'] = date('Y-m-d H:i:s', $item['createtime']);
            $item['user_nickname'] = $item['type']==2 ? Db::name('user')->where('id', $item['user_id'])->value('nickname') : '';
            return $item;
        });

        return $this->success('操作成功', $list);
    }

    public function index()
    {
        $length = input('length');
        $list = Db::name('card')->where('category_id', $this->shop_id)->limit($length, 30)->select();
        return $this->success('获取成功', $list);
    }

    public function edit()
    {
        $id = input('id');
        $data = Db::name('card')->where('category_id', $this->shop_id)->where('id', $id)->find();
        return $this->success('获取成功', $data);
    }

    public function add()
    {
        $card_name = (input('card_name'));
        $username = (input('username'));
        $card_no = input('card_no');
        if (!$card_name) {
            return $this->error('请选择账户');
        }
        if (!$username) {
            return $this->error('请输入收款人名字');
        }
        if (!$card_no) {
            return $this->error('请输入账号');
        }
        $res = Db::name('card')->insert([
            'category_id' => $this->shop_id,
            'card_name' => $card_name,
            'username' => $username,
            'card_no' => $card_no,
            'create_time' => time()
        ]);
        if ($res !== false) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }

    public function del()
    {
        $id = input('id');
        $res = Db::name('card')->where([
            'id' => $id,
            'category_id' => $this->shop_id,
        ])->delete();
        if ($res !== false) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }

    public function doedit()
    {
        $id = input('id');
        $card_name = (input('card_name'));
        $username = (input('username'));
        $card_no = input('card_no');
        if (!$card_name) {
            return $this->error('请选择账户');
        }
        if (!$username) {
            return $this->error('请输入收款人名字');
        }
        if (!$card_no) {
            return $this->error('请输入账号');
        }
        $res = Db::name('card')->where([
            'id' => $id,
            'category_id' => $this->shop_id,
        ])->update([
            'card_name' => $card_name,
            'username' => $username,
            'card_no' => $card_no,
        ]);
        if ($res !== false) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }
}
